//
//  LBMEmotionKeyboardView.swift
//  EmoticonKeyboard
//
//  Created by 刘保满 on 16/5/30.
//  Copyright © 2016年 itheima. All rights reserved.
// 键盘视图

import UIKit
import SnapKit

//键盘视图的高度
private let keyboardViewHeight: CGFloat = 220
//定义底部工具条的高度
private let toolBarHeight: CGFloat = 37

//定义重用标识符
private let emoticonCellId = "emoticonCellId"



class LBMEmotionKeyboardView: UIView {
    
    
    var packages = LBMEmoticonManager.sharedEmoticon.packages
    
    
    override init(frame: CGRect) {
        let rect = CGRect(x: 0, y: 0, width: 0, height: keyboardViewHeight)
        super.init(frame: rect)
        
        setupUI()
    }
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setupUI() {
        //添加底部工具条
        addSubview(toolbar)
        //添加表情视图
        addSubview(collectionView)
        //添加分页视图
        addSubview(pageControl)
        
        //设置约束
        toolbar.snp_makeConstraints { (make) in
            
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(toolBarHeight)
            
        }
        collectionView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
            
        }
        
        pageControl.snp_makeConstraints { (make) in
            
            make.left.right.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top).offset(-5)
            
            
        }
        
        
        //实现闭包
        toolbar.selectedIndexCallBack = { (index: Int) -> () in
            
            
            let indexPath = NSIndexPath(forItem: 0, inSection: index)
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            
            self.updatePageControl(indexPath)
        }
        
        
        //多线程也可以改变代码的执行顺序
        //当上面的代码执行完才会执行这段
        dispatch_async(dispatch_get_main_queue()) {
            //设置pageControl的初始值
            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
            self.updatePageControl(indexPath)
            
            
        }
        
        
        
        
        
        
    }
    
    
    //懒加载
    //底部工具条
    private lazy var toolbar: LBMEmoticonToolBar = LBMEmoticonToolBar()
    //表情视图
    private lazy var collectionView: UICollectionView = {
        
        //流布局
        let layout = UICollectionViewFlowLayout()
        //行列间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        layout.itemSize = CGSizeMake(UIScreen.mainScreen().bounds.width, keyboardViewHeight - toolBarHeight)
        
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor(white: 0.98, alpha: 1)
        //分页
        cv.pagingEnabled = true
        //弹簧效果
        cv.bounces = false
        cv.dataSource = self
        //设置代理
        cv.delegate = self
        //注册
        cv.registerClass(LBMEmoticonPageCell.self, forCellWithReuseIdentifier: emoticonCellId)
        return cv
    }()
    
    //MARK: pageControl
    private lazy var pageControl: UIPageControl = {
        let p = UIPageControl()
        
        //设置总页数
        p.numberOfPages = 6
        //设置当前页
        p.currentPage = 2
        //设置不可交互
        p.userInteractionEnabled = false
        
        //利用KVC设置分页中的图片
        //其他页数的图片
        p.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        //当前页的图片
        p.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        
        return p
    }()
    
    
}

//数据源方法
extension LBMEmotionKeyboardView: UICollectionViewDataSource ,UICollectionViewDelegate{
    
    //有多少组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    //每组有多少个
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].sectionEmoticon.count
    }
    //每组的每个有什么cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonCellId, forIndexPath: indexPath) as! LBMEmoticonPageCell
        
        cell.emoticons = packages[indexPath.section].sectionEmoticon[indexPath.item]
        
        return cell
        
    }
    
    //代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //通过collectionview的中心点来进行视图联动
        var center = collectionView.center
        //中心点加移动距离
        center.x = center.x + collectionView.contentOffset.x
        //判读当前屏幕显示的两个cell的frame 谁包含了这个中心点
        let indexPaths = collectionView.indexPathsForVisibleItems()
        for indexPath in indexPaths {
            let cell = collectionView.cellForItemAtIndexPath(indexPath)
            if cell!.frame.contains(center) {
                
                //如果包含了更新底部工具条和分页控制视图
                updatePageControl(indexPath)
                
                toolbar.setSelectedBtn(indexPath.section)
            }
            
        }
        
    }
    
    
    
    //用来更新分页视图
    private func updatePageControl(indexPath: NSIndexPath) {
        
        pageControl.numberOfPages = packages[indexPath.section].sectionEmoticon.count
        
        pageControl.currentPage = indexPath.item
        
    }
    
}










